package com.controller;


import com.DTO.GroupApplyDTO;
import com.Exception.NormalException.GroupException.UserAlreadyJoinGroupException;
import com.Exception.NormalException.NormalException;
import com.Result.GroupApplyResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.entity.Group;
import com.entity.GroupApply;
import com.entity.GroupUser;
import com.service.GroupApplyService;
import com.service.GroupService;
import com.service.GroupUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XTC
 * @since 2024-07-10
 */
@RestController
@RequestMapping("/groupApply")
@CrossOrigin
@Api(tags = "群聊申请管理器")
public class GroupApplyController {

    @Autowired
    private GroupApplyService groupApplyService;

    @Autowired
    private GroupUserService groupUserService;

    @Autowired
    private GroupService groupService;

    @PostMapping("/add")
    @ApiOperation(value = "发送加群申请")
    @ApiResponses({
            @ApiResponse(code = UserAlreadyJoinGroupException.code, message = UserAlreadyJoinGroupException.message)
    })
    public boolean add(@RequestBody GroupApplyDTO groupApplyDTO, HttpServletResponse response){
        try {
            GroupApply groupApply = new GroupApply();
            BeanUtils.copyProperties(groupApplyDTO,groupApply);
            groupApply.setApplyTime(new Date());
            groupApply.setStatus(0);
            this.groupApplyService.add(groupApply);
            return this.groupApplyService.sendGroupApply(groupApply);
        } catch (NormalException e) {
            e.setStatus(response);
            return false;
        }
    }

    @GetMapping("/getAll")
    @ApiOperation(value = "获取所有加群申请")
    public List<GroupApply> getAll() {
        return this.groupApplyService.list();
    }

    @GetMapping("/getApplyOfMyGroup")
    @ApiOperation(value = "获取我管理的群的申请")
    public List<GroupApplyResult> getApplyOfMyGroup(Integer userId) {
        List<Group> groupList = this.groupService.getMyGroup(userId);
        return this.groupApplyService.getApplyByGroupList(groupList);
    }

    @GetMapping("/acceptApply")
    @ApiOperation(value = "同意加群申请")
    public boolean acceptApply(Integer applyId) {
        this.groupApplyService.acceptApply(applyId);
        this.groupApplyService.sendApplyResult(applyId, true);
        return this.groupUserService.addUserToGroupByApplyId(applyId);
    }

    @GetMapping("/refuseApply")
    @ApiOperation(value = "拒绝加群申请")
    public boolean refuseApply(Integer applyId) {
        this.groupApplyService.refuseApply(applyId);
        this.groupApplyService.sendApplyResult(applyId, false);
        return true;
    }

    @GetMapping("/getByGroupId")
    @ApiOperation(value = "根据群聊获取群聊信息")
    public List<GroupApply> getByGroupId(Integer groupId) {
        QueryWrapper<GroupApply> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id", groupId);
        return this.groupApplyService.list(wrapper);
    }


}

